<?php

/**
 * winbinder 패키지 
 * 
 * 
 * @package php.ui.winbinder
 */

/**
 * WBControl
 * 
 * 
 *
 * @package php.ui.winbinder
 */
class WBControl extends WBComponent { 

	/**
	 * 생성자 
	 *
	 */
	public function __construct($options = array()) { 
		parent::__construct($options);
	}

	protected function createComponent() { 

		$tab = (isset($this->ntab)) ? $this->getOption('parent')->getTabIndex($this->getOption('ntab')) : $this->getOption('ntab') ;
		
		if ($this->getOption('width') <= 0) { $this->setOption('width', 10); }
		if ($this->getOption('height') <= 0) { $this->setOption('height', 10); }
		if ($this->getOption('x') <= 0) { $this->setOption('x', 0); }
		if ($this->getOption('y') <= 0) { $this->setOption('y', 0); }		
		
		if (!isset($this->caption)) { $this->setOption('caption', ""); }
		if (!isset($this->style)) { $this->setOption('style', 0); }
		
		$instance = wb_create_control(
				WBApplication::getInstance($this->getOption('parent')), 
				$this->getOption('class'), 
				$this->getOption('caption'), 
				$this->getOption('x'), 
				$this->getOption('y'), 
				$this->getOption('width'), 
				$this->getOption('height'), 
				$this->getOption('id'), 
				$this->getOption('style'), 
				$this->getOption('param'), 
				$tab
		);
		
		wbwindow($instance, $this);

		return $instance;
	}

	protected function initCreate() { 
		
		$defaultStyle = WBC_ENABLED ;

		if (isset($this->visible) && $this->getOption('visible') == false) { 
			$defaultStyle |= WBC_INVISIBLE;
		} else { 
			$defaultStyle |= WBC_VISIBLE;
		}
		$defaultParam = 0;

		$style = (isset($this->style)) ? $this->getOption('style') | $defaultStyle : $defaultStyle;
		$param = (isset($this->param)) ? $this->getOption('param') | $defaultParam : $defaultParam;

		$this->setOption('style', $style);
		$this->setOption('param', $param);
		
		if (!isset($this->autoVisible)) $this->setOption('autoVisible', true);
	}

	protected function initEvent() { 
		parent::initEvent();

		$this->setEvent('click',		'onClick');
		$this->setEvent('doubleclick',	'onDoubleClick');
		$this->setEvent('focus',		'onFocus');
		$this->setEvent('blur',			'onBlur');

	}

	// 컴포넌트 초기화 
	protected function initComponent() { 
		parent::initComponent();

		// 단축키 설정이 있을 때 
		if (isset($this->accel)) $this->setAccel($this->getOption('accel'));

		// 포커서 지정 
		if (isset($this->focus)) $this->focus();

		// 사용 불가능 지정 
		if (isset($this->disabled))	$this->setEnabled(!$this->getOption('disabled'));

		// 보이기 지정 
		//if (isset($this->visible))	$this->setVisible($this->getOption('visible'));

		// 테두리 지정 
		if (isset($this->border))	$this->setBorder($this->getOption('border'));

		// 넓이, 높이 지정이 안되어 있을 때는 미리 정해진 크기로 지정 
		if (!isset($this->width) || !isset($this->height)) { 
			$arr = $this->getPreferedSize();

			$this->setSize($arr['width'], $arr['height']);

		}
	}

	/**
	 * item 생성 
	 *
	 */
	public function createItems($items , $clear = false , $param = null) { 
		return wb_create_items($this->getInstance(), $items, $clear, $param);
	}

	/**
	 * 아이템 삭제 
	 *
	 */
	public function deleteItems($items = null) { 
		return wb_delete_items($this->getInstance(), $items);
	}

	/**
	 * 컨트롤 메모리 해제 
	 *
	 */
	public function destroy() { 
		return wb_destroy_control($this->getInstance());
	}

	/**
	 * 활성 여부 
	 *
	 * @return bool 
	 */
	public function isEnabled() { 
		return wb_get_enabled($this->getInstance()) ;
	}

	/**
	 * Treeview node depth 얻어오기 
	 *
	 * @param int $item TreeView node 
	 * @return int 
	 */
	public function getLevel($item) { 
		return wb_get_level($this->getInstance(), WBApplication::getInstance($item));
	}

	/**
	 * 선택된 index 얻어오기 
	 *
	 * @return int 
	 */
	public function getSelected() { 
		return wb_get_selected($this->getInstance());
	}

	/**
	 * TreeView , expanded, collapsed 표시 
	 *
	 * @return bool true : expanded, false : collpased 
	 */
	public function getState($item) { 
		return wb_get_state($this->getInstance(), $item);
	}

	/**
	 * value 설정 
	 *
	 * @param int $item 
	 * @param int $subitem 
	 * @return mixed  
	 */
	public function getValue($item = null, $subitem = null) { 
		return wb_get_value($this->getInstance(), $item, $subitem);
	}

	/**
	 * enabled 설정 
	 *
	 * @param bool $enabled
	 */
	public function setEnabled($enabled = true) { 
		return wb_set_enabled($this->getInstance(), $enabled);
	}

	/**
	 * 아이템 이미지 설정 
	 *
	 * @param int $index 
	 * @param int $item 
	 * @param int $subitem 
	 */
	public function setItemImage($index, $item = 0, $subitem = 0) { 
		return wb_set_item_image($this->getInstance(), $index, $item, $subitem);
	}

	/**
	 * set shortcut
	 *
	 * @param string $keybind  shortcut character ex) 'F1', '$' 
	 */
	public function setAccel($keyBind) {
		$parent = wbparent($this); 
		if ($parent instanceof WBWindow) {		// if $parent is WBWindow , set accel 
			$parent->setAccel($this->id, $keyBind, array($this, 'onClick'));
		}
	}

	/**
	 * selected 설정 
	 *
	 * @param int $items 
	 * @param bool $selected 
	 */
	public function setSelected($items, $selected = true) { 
		return wb_set_selected($this->getInstance(), $items, $selected);
	}

	/**
	 * TreeView 상태 설정 
	 *
	 * @param int $item 
	 * @param bool $state  
	 */
	public function setState($item, $state) { 
		return wb_set_state($this->getInstance(), $item, $state);
	}

	/**
	 * value 설정 
	 *
	 * @param mixed $value 
	 * @param int $item 
	 */
	public function setValue($value, $item = null) { 
		return wb_set_value($this->getInstance(), $value, $item);
	}

	public function setBorder($isBorder = true) { 
		return $this->setStyle(WBC_BORDER, $isBorder);
	}

	/**
	 * 범위 지정하기 
	 * 
	 * @param int $min 
	 * @param int max 
	 */
	public function setRange($min = 0, $max = 0) { 
		return wb_set_range($this->getInstance(), $min, $max);
	}

	/**
	 * 정렬하기 
	 *
	 * @param bool $asc true 이면 asc, false 이면 desc 
	 * @param bool $subitem 
	 */
	public function sort($asc = true , $subitem = null) { 
		return wb_sort($this->getInstance(), $asc, $subitem);
	}

	/**
	 * 이벤트 실행 
	 *
	 * @param WBEvent $eo 이벤트 객체 
	 */
	public function fire($eo) {
			
		// focus
		if ($eo->isFocus()) { $this->fireEvent('focus', $eo); return; }

		// double click 
		if ($eo->isDoubleClick()) { $this->fireEvent('doubleclick', $eo); return; }

		// default click event 
		$this->fireEvent('click', $eo); 
	}

	/**
	 * click 이벤트 실행 
	 *
	 */
	public function click() { 
		// WM_COMMAND, MAKELONG(ID, BN_CLICKED), HWND
		// ::SendMessage(hwndParent, WM_COMMAND, MAKELONG(nButtonID, BN_CLICKED), hwndButton);

		$this->send(WBUser::WM_COMMAND, ($this->getOption('id') | (WBUser::BN_CLICKED << 16)), $this->getHwnd()->hwnd);
	}

	public function onRedraw(WBEvent $eo) { 
		$this->repaint($this->getGraphics());
	}
	
	public function getGraphics($eo = null) { 
		return new WBGraphics($this->getInstance());
	}
	
	/**
	public function setMinimumSize($width = 0, $height = 0) {
		parent::setMinimumSize($width, $height);

		$this->setArea(WBC_MINSIZE, 0, 0, $width, $height);
	}

	public function setMaximumSize($width = 0, $height = 0) {
		parent::setMaximumSize($width, $height);

		$this->setArea(WBC_MAXSIZE, 0, 0, $width, $height);
	}
	**/
}

?>